package com.meinv.auto;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import com.joke.util.DownloadUtil;
import com.joke.util.HttpClientGet;


@Service
public class VulgarContentFetch {
	
	public static void main(String[] args) {
		new VulgarContentFetch().fetchEmotion();
	}

	private static String wap = "http://m.miss-no1.com";
	
//    private static String missnoBoth = "http://life.miss-no1.com/file/2016/11/16/662810@093956_4.htm";  //187

	private static String missnoBoth = "http://life.miss-no1.com/errenshijie/wuchiwenda/(*).html";  //187
	private static String missnoBoth1 = "http://life.miss-no1.com/errenshijie/xingwenbaodao/(*).html";  //281 bad
	private static String missnoBoth2 = "http://life.miss-no1.com/errenshijie/waiguoxingwenhua/(*).html"; //53 bad
	private static String missnoBoth3 = "http://life.miss-no1.com/errenshijie/xingzishi/(*).html";  //109 good
	private static String missnoBoth4 = "http://life.miss-no1.com/errenshijie/nanxingxingxinli/(*).html";//94 
	private static String missnoBoth5 = "http://life.miss-no1.com/errenshijie/xiandaixingwenhua/(*).html";//70
	private static String missnoBoth6 = "http://life.miss-no1.com/errenshijie/xingbaojianyinshi/(*).html";//46
	private static String missnoBoth7 = "http://life.miss-no1.com/errenshijie/qingshaonianxingbaojian/(*).html";//64
	private static String missnoBoth8 = "http://life.miss-no1.com/errenshijie/aifujiqiao/(*).html";//120
	private static String missnoBoth9 = "http://life.miss-no1.com/errenshijie/biyunchangshi/(*).html";//130
	private static String missnoBoth10 = "http://life.miss-no1.com/errenshijie/lingleichangshi/(*).html";//122
	private static String missnoBoth11 = "http://life.miss-no1.com/errenshijie/chengrenhuati/(*).html";//630

	private static String mnCookie = "UM_distinctid=162f767a78e10b-0f8aac16c730b3-3b604c04-100200-162f767a78f294; Hm_lvt_8140efcd2b2062e7e763b120d4e54644=1527506244,1527588573; ASPSESSIONIDCCQRRCTB=HEIMMPGCFPGLPGJBPBDECGPC; CNZZDATA1635892=cnzz_eid%3D1220004858-1527506030-null%26ntime%3D1527668728; Hm_lpvt_8140efcd2b2062e7e763b120d4e54644=1527673875";
    String writeImagePath="D:/sese/情感url.txt";

	public void fetchEmotion() {
		Map<String, String> imageMap = new HashMap<>();
		missnoImage(imageMap);
//		log.info("抓取的<情感>条数："+imageMap.size());
		//保存入库趣图
		DownloadUtil.writeImage(writeImagePath, imageMap);
		System.out.println("抓取的<情感>条数："+imageMap.size());
//		for (String key : imageMap.keySet()) {
////			log.info(key);
////			log.info(imageMap.get(key));
//			System.out.println(key);
//			System.out.println(imageMap.get(key));
//		}
	}
	
	private Map<String, String> missnoImage(Map<String, String> imageMap) {
		for (int i = 1; i < 20; i++) {//187
			String contentHTML = getMNContent(missnoBoth,mnCookie,i);
//			System.out.println(contentHTML);
			parseMNImageContent(contentHTML,imageMap,i,missnoBoth);
		}
/*				for (int i = 1; i < 281; i++) {
			String contentHTML = getMNContent(missnoBoth1,mnCookie,i);
//			System.out.println(contentHTML);
			parseMNImageContent(contentHTML,imageMap,i,missnoBoth1);
		}
				for (int i = 1; i < 53; i++) {
			String contentHTML = getMNContent(missnoBoth2,mnCookie,i);
//			System.out.println(contentHTML);
			parseMNImageContent(contentHTML,imageMap,i,missnoBoth2);
		}
				for (int i = 1; i < 109; i++) {
			String contentHTML = getMNContent(missnoBoth3,mnCookie,i);
//			System.out.println(contentHTML);
			parseMNImageContent(contentHTML,imageMap,i,missnoBoth3);
		}
				for (int i = 1; i < 94; i++) {
			String contentHTML = getMNContent(missnoBoth4,mnCookie,i);
//			System.out.println(contentHTML);
			parseMNImageContent(contentHTML,imageMap,i,missnoBoth4);
		}
				for (int i = 1; i < 70; i++) {
			String contentHTML = getMNContent(missnoBoth5,mnCookie,i);
//			System.out.println(contentHTML);
			parseMNImageContent(contentHTML,imageMap,i,missnoBoth5);
		}
				for (int i = 1; i < 46; i++) {
			String contentHTML = getMNContent(missnoBoth6,mnCookie,i);
//			System.out.println(contentHTML);
			parseMNImageContent(contentHTML,imageMap,i,missnoBoth6);
		}
				for (int i = 1; i < 64; i++) {
			String contentHTML = getMNContent(missnoBoth7,mnCookie,i);
//			System.out.println(contentHTML);
			parseMNImageContent(contentHTML,imageMap,i,missnoBoth7);
		}
				for (int i = 1; i < 120; i++) {
			String contentHTML = getMNContent(missnoBoth8,mnCookie,i);
//			System.out.println(contentHTML);
			parseMNImageContent(contentHTML,imageMap,i,missnoBoth8);
		}
				for (int i = 1; i < 130; i++) {
			String contentHTML = getMNContent(missnoBoth9,mnCookie,i);
//			System.out.println(contentHTML);
			parseMNImageContent(contentHTML,imageMap,i,missnoBoth9);
		}
				for (int i = 1; i < 122; i++) {
			String contentHTML = getMNContent(missnoBoth10,mnCookie,i);
//			System.out.println(contentHTML);
			parseMNImageContent(contentHTML,imageMap,i,missnoBoth10);
		}
			for (int i = 1; i < 630; i++) {
			String contentHTML = getMNContent(missnoBoth11,mnCookie,i);
//			System.out.println(contentHTML);
			parseMNImageContent(contentHTML,imageMap,i,missnoBoth11);
		}*/

		return imageMap;
	}

	private String getMNContent(String url,String cookie, int i) {
		HttpClientGet.createDefaultHttpClient();
		Map<String, String> header = new HashMap<>();
		header.put("Cookie",cookie);
		HttpClientGet.setHeader(header);
		if(i==0) return null;
		else if(i>0) url = url.replace("(*)", String.valueOf(i));
		String contentHTML = HttpClientGet.sendHttpGet(url);
		return contentHTML;
	}

	private Map<String, String> parseMNImageContent(String contentHTML,Map<String, String> imageMap,int page,String suf) {
		if(contentHTML==null||"".equals(contentHTML)) return imageMap;
		Document doc1 = Jsoup.parse(contentHTML); 
		Elements links2 = doc1.select("[class=lest]");
		Elements imagePath = links2.select("a");
		int i = 0;
		for (Element element : imagePath) {
			String articleUrl = element.attr("href");
			String articleTitle = element.attr("title");
//			System.out.println(articleTitle);
//			System.out.println(articleUrl);
			imageMap.put(articleTitle, articleUrl);
			String comurl = articleUrl;
//			System.out.println(articleUrl);

			//处理url
			int a = articleUrl.lastIndexOf("/");
			int b = articleUrl.lastIndexOf("@");
			int c = articleUrl.lastIndexOf("_");
			String aaa = articleUrl.substring(b,c);
//			System.out.println("aaa:"+aaa);
			articleUrl = articleUrl.replace(articleUrl.substring(0, a), wap);
			articleUrl = articleUrl.replace(aaa, "");
//			articleUrl = articleUrl + suff;
			System.out.println(articleUrl);
			HttpClientGet.createDefaultHttpClient();
//			String AcontentHTML = HttpClientGet.sendHttpGet(articleUrl);
			String AcontentHTML = HttpClientGet.sendHttpGet(comurl);

//			System.out.println(AcontentHTML);
			parseMNImageContent(AcontentHTML);
			//TODO 处理抓取的内容
				//要有图片的
				//取一到两张图片做封面
				//取页数，拼接页数
				//拼接内容
			return imageMap;
		}

		return imageMap;
	}
	
	//TODO 解析处理抓取的内容
	//要有图片的
	//取一到两张图片做封面
	//取页数，拼接页数
	//拼接内容
	private String parseMNImageContent(String contentHTML) {
		if(contentHTML==null||"".equals(contentHTML)) return null;
		int pn = contentHTML.indexOf("pn=");
		int page = Integer.valueOf(contentHTML.substring(pn+3, pn+4));
		System.out.println("page="+page);
		Document doc1 = Jsoup.parse(contentHTML); 
		Elements links2 = doc1.select("script");
		String script = links2.text();
		
		Elements links3 = doc1.select("[class=content]");
		String content = links3.html();
//		System.out.println(script);
        String regEx_html="<a[^>]+>"; //定义HTML标签的正则表达式 搜索
        String regEx_html1="<(a|span)[^>]*?>"; //定义HTML标签的正则表达式 搜索 [\\s\\S]*?<\\/a>
//        String regEx_html2="<span[^>]*?>[\\s\\S]*?<\\/span>"; //定义HTML标签的正则表达式 搜索

        content = content.replace("</a>", "").replace("</span>", "");

		 Pattern p_html=Pattern.compile(regEx_html1,Pattern.CASE_INSENSITIVE); 
         Matcher m_html=p_html.matcher(content); 
         content=m_html.replaceAll(""); //过滤html标签 
//
//		 Pattern p_html2=Pattern.compile(regEx_html2,Pattern.CASE_INSENSITIVE); 
//         Matcher m_html2=p_html2.matcher(content); 
//         content=m_html2.replaceAll(""); //过滤html标签 
         
		System.out.println("--:"+content);
		return null;
	}
}
